home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / utilities / csinvals.lha / CSinVals / CSinVals.c next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  7.3 KB  |  306 lines

  1. /*
  2.     Create Sinus values
  3.     ------------------------
  4.     (c)Codex Design Software
  5.     15.9.1994
  6. */
  7.  
  8. #include    <string.h>
  9. #include    <stdio.h>
  10. #include    <math.h>
  11.  
  12. #include    <dos/dos.h>
  13. #include    <proto/dos.h>
  14.  
  15. // ------------------------------------
  16.  
  17. #define    PROGNAME        "CSinVals"
  18. #define    PROGVERSION    "V1.00"
  19.  
  20. #define    ARG_FILE            0
  21. #define    ARG_LABEL        1
  22. #define    ARG_COSLAB        2
  23. #define    ARG_START        3
  24. #define    ARG_COUNT        4
  25. #define    ARG_STEP            5
  26. #define    ARG_MULTI        6
  27. #define    ARG_ADD            7
  28. #define    ARG_CHAR            8
  29. #define    ARG_SHORT        9
  30. #define    ARG_LONGINT        10
  31. #define    ARG_OVERWRITE    11
  32. #define    ARG_ARGCNT        12
  33.  
  34. #define    DEF_FILE            0
  35. #define    DEF_LABEL        (long)"SinValues"
  36. #define    DEF_COSLAB        0
  37. #define    DEF_START        0
  38. #define    DEF_COUNT        -1
  39. #define    DEF_STEP            1
  40. #define    DEF_MULTI        256
  41. #define    DEF_ADD            0
  42. #define    DEF_CHAR            FALSE
  43. #define    DEF_SHORT        TRUE
  44. #define    DEF_LONGINT        FALSE
  45. #define    DEF_OVERWRITE    FALSE
  46.  
  47. #define    VALUES_PER_LINE    10
  48.  
  49. // write mit Fehlerabfrage
  50. #define    WRITESTR(STR)        if( Write(File,STR,Dummy = strlen(STR)) != Dummy ) { PrintFault(IoErr(),"Can't save data: "); goto EndUp; }
  51.  
  52. // ------------------------------------
  53.  
  54. static char    Template[]        =    "C-FILE/A,"
  55.                                         "LABEL/K,"
  56.                                         "COSLAB/K,"
  57.                                         "START/K/N,"
  58.                                         "COUNT/K/N,"
  59.                                         "STEP/K/N,"
  60.                                         "MULTI/K/N,"
  61.                                         "ADD/K/N,"
  62.                                         "CHAR/S,SHORT/S,LONGINT/S,"
  63.                                         "OVERWRITE/S",
  64.                 ExtHelpTxt[]    =    "\n"
  65.                                         "Codex Design Software presents:\n"
  66.                                         " " PROGNAME " " PROGVERSION "\n"
  67.                                         "\n"
  68.                                         " This programm creates an ASCII C-Source with Sinus-Values.\n"
  69.                                         "It is dedicated to those who want very fast Sin() & Cos()\n"
  70.                                         "calculations.\n"
  71.                                         "\n"
  72.                                         "C-FILE   : Target file; only thing you MUST pass !\n"
  73.                                         "\n"
  74.                                         "LABEL    : Label for start of sinus values [SinValues].\n"
  75.                                         "COSLAB   : Label for start of cosinus values [<none>].\n"
  76.                                         "           Be careful ! This program assumes that two data\n"
  77.                                         "           fields wrote behind each other will be compiled\n"
  78.                                         "           behind each other in the final program, too.\n"
  79.                                         "           If you are not sure, use:\n"
  80.                                         "           #define CosLab[OFF] SinLab[(90/<STEP>) + OFF] !\n"
  81.                                         "\n"
  82.                                         "START    : Start by this angel [0°].\n"
  83.                                         "COUNT    : Create that many values [360/STEP].\n"
  84.                                         "STEP     : Difference between two neighboured values [1].\n"
  85.                                         "\n"
  86.                                         "MULTI    : Multiply values by this number [256].\n"
  87.                                         "ADD      : Add this number to values [0].\n"
  88.                                         "\n"
  89.                                         "CHAR,SHORT,LONGINT : Type of data [SHORT].\n"
  90.                                         "\n"
  91.                                         "OVERWRITE: Supress warning if file exists [FALSE].\n"
  92.                                         "\n"
  93.                                         "(c)15.9.1994 by Hans Bühler, Codex Design Software.\n"
  94.                                         "all rights reserved; free for copying.";
  95.  
  96. static LONG        TempArray[ARG_ARGCNT]    =    {    DEF_FILE,
  97.                                                             DEF_LABEL,
  98.                                                             DEF_COSLAB,
  99.                                                             0,
  100.                                                             0,
  101.                                                             0,
  102.                                                             0,
  103.                                                             0,
  104.                                                             DEF_CHAR,
  105.                                                             DEF_SHORT,
  106.                                                             DEF_LONGINT,
  107.                                                             DEF_OVERWRITE
  108.                                                         };
  109.  
  110. static char        *Types[3]    =    {    "char ",
  111.                                             "short ",
  112.                                             "long int "
  113.                                         };
  114.  
  115. // ------------------------------------
  116.  
  117. void main(void)
  118. {
  119.     struct RDArgs    *Anchor;
  120.     double            Multi,Add,Buf;
  121.     int                Count,i,k,Start,
  122.                         Step,
  123.                         CosLabelOff,
  124.                         NLCnt,Dummy;
  125.     char                *Type,
  126.                         Buffer[20];
  127.     BPTR                File;
  128.     BOOL                Pred;
  129.  
  130.     // -- parse arguments --
  131.  
  132.     if(!( Anchor = ReadArgs(Template,TempArray,0) ))
  133.     {
  134.         puts(ExtHelpTxt);
  135.         return;
  136.     }
  137.  
  138.     Start        =    (TempArray[ARG_START]) ? *((long *)(TempArray[ARG_START])) : DEF_START;
  139.     Count        =    (TempArray[ARG_COUNT]) ? *((long *)(TempArray[ARG_COUNT])) : DEF_COUNT;
  140.     Step        =    (TempArray[ARG_STEP] ) ? *((long *)(TempArray[ARG_STEP]))  : DEF_STEP;
  141.     Multi        =    (TempArray[ARG_MULTI]) ? *((long *)(TempArray[ARG_MULTI])) : DEF_MULTI;
  142.     Add        =    (TempArray[ARG_ADD]  ) ? *((long *)(TempArray[ARG_ADD]))   : DEF_ADD;
  143.  
  144.     if(!Step || !Multi || !Count)
  145.     {
  146.         puts("ERROR: Invalid parameters (STEP,MULTI,COUNT == 0) !");
  147.         goto EndUp;
  148.     }
  149.  
  150.     if(Count == -1)
  151.         Count    =    360 / Step;
  152.  
  153.     if(TempArray[ARG_CHAR])
  154.         Type    =    Types[0];
  155.     else
  156.         if(TempArray[ARG_LONGINT])
  157.             Type    =    Types[2];
  158.         else
  159.             Type    =    Types[1];
  160.  
  161.     if(TempArray[ARG_COSLAB])
  162.     {
  163.         if(90 % Step)
  164.         {
  165.             printf(    "NOTE: Can't create Cosinus-Label '%s' since dividing\n"
  166.                         "      90° by Step=%ld is difficult for integers.\n"
  167.                         "      Label won't be set !\n",TempArray[ARG_COSLAB],Step);
  168.             CosLabelOff    =    Count;    // disable for(;;)
  169.         }
  170.         else
  171.         {
  172.             CosLabelOff    =    90 / Step;
  173.             Count            +=    CosLabelOff;        // make that more !
  174.         }
  175.     }
  176.     else
  177.         CosLabelOff    =    Count;        // disable for(;;)
  178.  
  179.     // -- open file & write header --
  180.  
  181.     printf("Opening '%s'.... ",TempArray[ARG_FILE]);
  182.  
  183.     if(!TempArray[ARG_OVERWRITE])
  184.         if(File = Open((char *)TempArray[ARG_FILE],MODE_OLDFILE))
  185.         {
  186.             Close(File);
  187.             printf("does already exits;\nDelete old file [n]: ");
  188.             scanf("%s",ExtHelpTxt);
  189.             if((ExtHelpTxt[0] | ' ') != 'y')
  190.                 goto EndUp;
  191.             printf("Deleting old file... ");
  192.         }
  193.  
  194.     if(!( File = Open((char *)TempArray[ARG_FILE],MODE_NEWFILE) ))
  195.     {
  196.         PrintFault(IoErr(),PROGNAME " can't open file: ");
  197.         goto EndUp;
  198.     }
  199.  
  200.     printf("done.\nSaving header... ");
  201.  
  202.     sprintf(ExtHelpTxt,    "/* %ld SinValues starting by %ld°, step %ld,\n"
  203.                                 "   multiplied by %ld, added by %ld\n"
  204.                                 "   ---\n"
  205.                                 "   Created using " PROGNAME " " PROGVERSION "\n"
  206.                                 "   (c)1994 Codex Design Software */\n"
  207.                                 "\n",
  208.                                 Count,
  209.                                 Start,
  210.                                 Step,
  211.                                 (long)Multi,
  212.                                 (long)Add);
  213.  
  214.     WRITESTR(ExtHelpTxt);
  215.     WRITESTR(Type);                                    // write 'short '
  216.     WRITESTR((char *)TempArray[ARG_LABEL]);
  217.     WRITESTR("[] = {\n  ");
  218.  
  219.     // -- make first load --
  220.  
  221.     printf("done.\nSaving sin data... ");
  222.  
  223.     Pred                =    FALSE;
  224.     ExtHelpTxt[0]    =    0;
  225.  
  226.     for(NLCnt=0, i=0; i<CosLabelOff; i++)
  227.     {
  228.         Buf    =    sin((double)((PI*2) * (double)Start) / 360);
  229.         Buf    *=    Multi;
  230.         Buf    +=    Add;
  231.         k        =    Buf;                    // translate to int
  232.         Start    =    (Start + Step) % 360;
  233.  
  234.         if(NLCnt == VALUES_PER_LINE)
  235.         {
  236.             strcat(ExtHelpTxt,"\n  ");
  237.             WRITESTR(ExtHelpTxt);
  238.             ExtHelpTxt[0] = 0;
  239.             NLCnt    =    0;
  240.         }
  241.  
  242.         sprintf(Buffer,"%ld,",k);
  243.         strcat(ExtHelpTxt,Buffer);
  244.         NLCnt++;
  245.     }
  246.  
  247.     if(NLCnt)                                            // falls CosLabelOff == 0
  248.     {
  249.         ExtHelpTxt[strlen(ExtHelpTxt)-1] = 0;    // kill last commata !
  250.         strcat(ExtHelpTxt,"\n }");
  251.         WRITESTR(ExtHelpTxt);
  252.     }
  253.  
  254.     // -- generate cos label ? --
  255.  
  256.     if(i < Count)                                        // still something to do ?
  257.     {
  258.         printf("done.\nSaving cosinus data... ");
  259.  
  260.         WRITESTR(",\n ");
  261.         WRITESTR((char *)TempArray[ARG_COSLAB]);
  262.         WRITESTR("[] = {\n  ");
  263.  
  264.         Pred                =    FALSE;
  265.         ExtHelpTxt[0]    =    0;
  266.  
  267.         for(NLCnt=0; i<Count; i++)
  268.         {
  269.             Buf    =    sin((double)((PI*2) * (double)Start) / 360);
  270.             Buf    *=    Multi;
  271.             Buf    +=    Add;
  272.             k        =    Buf;                    // translate to int
  273.             Start    +=    Step;
  274.  
  275.             if(NLCnt == VALUES_PER_LINE)
  276.             {
  277.                 strcat(ExtHelpTxt,"\n  ");
  278.                 WRITESTR(ExtHelpTxt);
  279.                 ExtHelpTxt[0] = 0;
  280.                 NLCnt    =    0;
  281.             }
  282.  
  283.             sprintf(Buffer,"%ld,",k);
  284.             strcat(ExtHelpTxt,Buffer);
  285.             NLCnt++;
  286.         }
  287.  
  288.         if(NLCnt)                                            // falls Count == 0 (unmöglich, aber na ja !)
  289.         {
  290.             ExtHelpTxt[strlen(ExtHelpTxt)-1] = 0;    // kill last commata !
  291.             strcat(ExtHelpTxt,"\n }");
  292.             WRITESTR(ExtHelpTxt);
  293.         }
  294.     }
  295.  
  296.     // -- finish --
  297.  
  298.     WRITESTR(";\n\n/* " PROGNAME " " PROGVERSION " */\n");
  299.  
  300.     Close(File);
  301.     puts("done.\nOperation successful.");
  302.  
  303. EndUp:
  304.  
  305.     FreeArgs(Anchor);
  306. }